package com.example.birdfriends.service.impl;

import com.example.birdfriends.dao.PostDao;
import com.example.birdfriends.pojo.hot.HotPost;
import com.example.birdfriends.pojo.post.Post;
import com.example.birdfriends.service.PostService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostDao postDao = null;

    @Override
    public int createPost(Post post) {  // 用户创建帖子
        return postDao.insertPost(post);
    }

    @Override
    public boolean compareUid(Long pid, Long curLoginUid) {
        Long authorId = postDao.findUidByPid(pid);  // 找到帖子作者的id
        System.out.println("帖子id："+pid+"; 帖子作者id："+authorId);
        return curLoginUid.equals(authorId);
    }

    // 查找需要修改的帖子
    @Override
    public Post findPostByPid(Long pid) {
        return postDao.findPostByPid(pid);
    }

    @Override
    public int updatePostByPid(Post post) {
        return postDao.updatePostByPid(post);
    }

    @Override
    public int deletePostByPid(Long pid) {
        return postDao.deletePostById(pid);
    }

    @Override
    public List<Post> queryPostWithLoginInMain(Long uid, Long start, Long lineNumber) {
        return postDao.queryPostWithLoginInMain(uid, start, lineNumber);
    }

    @Override
    public List<Long> findPostIdsOrderByPid() {
        return postDao.findPostsIdsOrderByPid();
    }

    @Override
    public Long countPostsWithLogin(Long curLoginUid) {
        return postDao.countPostsWithLogin(curLoginUid);
    }

    @Override
    public List<Post> findPostsByUid(Long uid) {
        return postDao.findPostsByUid(uid);
    }

    @Override
    public List<Post> queryAllPosts(Long start, Long lineNumber) {
        return postDao.queryAllPosts(start, lineNumber);
    }

    @Override
    public int setImagesAndVideos(Post post) {
        return postDao.setImagesAndVideos(post);
    }

    @Override
    public Long countPostByThemeNotLogin(String theme) {
        return postDao.countPostByThemeNotLogin(theme);
    }

    @Override
    public List<Post> findPostsByThemeNotLogin(String theme, Long start, Long lineNumber) {
        return postDao.findPostsByThemeNotLogin(theme, start, lineNumber);
    }

    @Override
    public List<Post> findPostsByThemeWithLogin(String theme, Long uid,Long start, Long lineNumber) {
        return postDao.findPostsByThemeWithLogin(theme, uid, start, lineNumber);
    }

    @Override
    public Long countPostByThemeWithLogin(String theme, Long uid) {
        return postDao.countPostByThemeWithLogin(theme, uid);
    }

    @Override
    public List<Post> findPostsByTitleOrContent(String title, String content) {
//        System.out.println("根据标题或内容搜索："+title+ " " + content);
        return postDao.findPostsByTitleOrContent(title, content);
    }

    @Override
    public Long countPostsNotLogin() {
        return postDao.countPostsNotLogin();
    }

    @Override
    public List<Long> findPostIdsWithLogin(Long curLoginUid){
        return postDao.findPostIdListWithLogin(curLoginUid);
    }

    @Override
    public List<Post> findHotPostIdList() {
        List<HotPost> hotPosts = postDao.findHotPostsTop100();
        List<Post> posts = new ArrayList<>();
        for (HotPost hotPost: hotPosts){
            posts.add(findPostByPid(hotPost.getPid()));
        }
        return posts;
    }

    @Override
    public List<Post> findHotPostIdListInToday(Timestamp curTime) {
        return null;
    }
}
